#include "StdAfx.h"
#include ".\mfcwpfrect.h"

CMFCWPFRect::CMFCWPFRect(void)
{
}

CMFCWPFRect::~CMFCWPFRect(void)
{
}

///<summary>
///Get Node Attributes from the xaml file using the path and
///calculate the element's rect
///</summary>
///<param name='szPath'>
///the path in the xaml file
///</param>
///<param name='pXmlParser'>
///the pointer to the xml parser
///</param>
///<param name='oRect'>
///the region of the element that contains this element
///</param>
void CMFCWPFRect::Parse(const CString & szPath, CECGXmlParser * pXamlParser, const CRect & oRect)
{
	CMFCWPFElement::Parse(szPath, pXamlParser, oRect);
	CalculateRect(oRect);
}

///<summary>
///calculate the element's rect
///</summary>
///<param name='oRect'>
///the region of the element that contains this element
///</param>
void CMFCWPFRect::CalculateRect(const CRect & oRect)
{
	m_oRect = oRect;
	int nLeft = 0;
	int nTop = 0;
	int nRight = 0;
	int nBottom = 0;
	if (m_oAttributes.GetAttribute(_ATTRIBUTELEFT, nLeft) &&
		m_oAttributes.GetAttribute(_ATTRIBUTETOP, nTop) &&
		m_oAttributes.GetAttribute(_ATTRIBUTERIGHT, nRight) &&
		m_oAttributes.GetAttribute(_ATTRIBUTEBOTTOM, nBottom))
	{
		m_oRect.SetRect(oRect.left+nLeft, oRect.top+nTop, oRect.right-nRight, oRect.bottom-nBottom);
	}
}

///<summary>
///Draw the element's background in the context of pCDCPlus 
///according to the "IsEnabled" attribute
///</summary>
///<param name='pCDCPlus'>
///the pointer to the GDI+ display context
///</param>
///<returns>
///true if the element is enabled; false otherwise
///</returns>
bool CMFCWPFRect::DrawSelf(CDCPlus * pCDCPlus)
{
	if (CMFCWPFElement::DrawSelf(pCDCPlus))
	{
		CRgn oRgn;
		oRgn.CreateRectRgnIndirect(m_oRect);
		CString szColorName;
		m_oAttributes.GetAttribute(_ATTRIBUTEBACKGROUND, szColorName);
		pCDCPlus->SetBrushPlus(m_oAttributes.GetColorFromName(szColorName), CBrushLib::BRUSH_TYPE_SOLID);
		pCDCPlus->FillRgnPlus(&oRgn);
		return true;
	}
	return false;
}

///<summary>
///using the element's rect to calculate the element is clicked or not
///</summary>
///<param name='nFlags'>
///show if there is other keys pushing down
///</param>
///<param name='oPoint'>
///the pointer 
///</param>
///<returns>
///the pointer to the element
///</returns>
CMFCWPFElement * CMFCWPFRect::MouseLeftButtonUp(int /*nFlags*/, const CPoint & oPoint)
{
	CMFCWPFElement * pMFCWPFElement = NULL;
	if (IsEnabled())
	{
		if (m_oRect.PtInRect(oPoint))
		{
			pMFCWPFElement = this;
		}
	}
	return pMFCWPFElement;
}

///<summary>
///Get the element's rect
///</summary>
///<returns>
///the rect of the element
///</returns>
CRect CMFCWPFRect::GetRect()
{
	return m_oRect;
}